<div class="container-fluid">
    <div class="row">
        <div class="col-lg-12">
        	<a onclick='onAdd()' class='btn btn-primary'>添加代理</a>
            <div class="card">
                <div class="card-body">
                    <table id="table"></table>
                </div>
            </div>
        </div>
    </div>
</div> 

<style>
.control-label {
    padding-top: 7px;
}
</style>

<script>
var columns = [{
    field: 'ptitle',
    title: '推荐人'
},{
    field: 'title',
    title: '代理名称'
},{
    field: 'leveltitle',
    title: '代理等级'
}, {
    field: 'mobile',
    title: '手机号'
}, {
    field: 'credit',
    title: '当前余额',
    formatter: function(value,item){ // 自定义方法
		return "<a class='btn btn-primary btn-sm' target='_blank' href='/SPAdmin/agent?logaid="+item.id+"'>"+item.credit+"</a>"
    }
}, {
    field: 'num',
    title: '下级数'
}, {
    field: 'operate',
    title: '操作',
    formatter: function(value,item){ // 自定义方法
        let html = '';
        html += '<div class="btn-group">';
            html += '<a class="btn btn-xs btn-default edit-btn" data-toggle="tooltip" title="编辑">编辑</a>';
            html += '<a class="btn btn-xs btn-primary credit-btn" data-toggle="tooltip" title="充值">充值</a>';
            html += '<a class="btn btn-xs btn-danger del-btn" data-toggle="tooltip" title="删除">删除</i></a>';
        html += '</div>';
        return html;
    }, 
    events: {
        'click .edit-btn': function (event, value, row, index) {
            event.stopPropagation();
            onAdd(row)
        },
        'click .del-btn': function (event, value, row, index) {
            event.stopPropagation();
            $.confirm({
        		title:'删除代理',
        		content: '确定删除代理？',
                buttons:{
                	confirm:{ 
                		text:'确认',
                		btnClass: "btn-primary",
                		action: function(){
                			showLoading();
        	                $.ajax({
        	                	url:"/SPAdmin/agent/del",
        	                	type:'post',
        	                	dataType:'json',
        	                	data:{id:row.id},
        	                	success:function(res){
        	                		hideLoading();
        	                		if(res.code == 1){
        	                			$.notify('删除成功');
                                        $('#table').bootstrapTable('refresh')
        	                		}else $.notify(res.msg);
        	                	}
        	                });
        	            },
                	},
                	cancel:{text:'取消'}
                }
        	});
        },
        'click .credit-btn': function (event, value, row, index) {
            event.stopPropagation();
            var str = "";
        	str += '<div class="input-group" style="margin-bottom: 10px;">'
        	str += '<div class="input-group-text">余额变化</div>'
        	str += '<input type="number" class="form-control" placeholder="负数为扣除">'
        	str += '</div>'
        	str += '<div class="input-group" style="margin-bottom: 10px;">'
        	str += '<div class="input-group-text">变化备注</div>'
        	str += '<input type="text" class="form-control">'
        	str += '</div>'
            $.confirm({
        		title:'余额充值',
        		content: str,
                buttons:{
                	confirm:{ 
                		text:'确认',
                		btnClass: "btn-primary",
                		action: function(){
                    		var credit = this.$content.find('input').eq(0).val();
                    		if(credit == 0 || credit == '') return $.alert('请输入变化的余额');
                    		var remark = this.$content.find('input').eq(1).val();
                			showLoading();
        	                $.ajax({
        	                	url:"/SPAdmin/agent",
        	                	type:'post',
        	                	dataType:'json',
        	                	data:{aid:row.id,op:'credit',credit:credit,remark:remark},
        	                	success:function(res){
        	                		hideLoading();
        	                		if(res.code == 1){
        	                			$.notify('操作成功');
                                        $('#table').bootstrapTable('refresh')
        	                		}else $.notify(res.msg);
        	                	}
        	                });
        	            },
                	},
                	cancel:{text:'取消'}
                }
        	});
        },
    }
}];
$('#table').bootstrapTable({
    url: '/SPAdmin/agent',
    dataType: 'json',
	method: 'post',
    pagination: true,//开启分页
    pageSize:15,
    pageNumber:1,
    sidePagination:'server',
    search: false, //开启刷选
    columns: columns,
    queryParams:function(params) {
        /******获取分页数据时，这里你可以带上你自定义的参数,bootstrap table会把这些参数合并到请求里******/
       return params;
	}
});

function onAdd(item){
	var str = "";
	str += '<div class="input-group" style="margin-bottom: 10px;">'
	str += '<div class="input-group-text">代理名称</div>'
	str += '<input type="text" maxlength="6" class="form-control" value="'+(item ? item.title : '')+'">'
	str += '</div>'
	str += '<div class="input-group" style="margin-bottom: 10px;">'
	str += '<div class="input-group-text">代理等级</div>'
	str += '<select class="form-control">'
	<?php foreach ($levels as $l):?>
	str += '<option value="<?=$l['level']?>" '+(item && item.level == <?=$l['level']?> ? 'selected' : '')+'><?=$l['title']?></option>'
	<?php endforeach;?>
	str += '</select>'
	str += '</div>'
	str += '<div class="input-group" style="margin-bottom: 10px;">'
	str += '<div class="input-group-text">代理手机</div>'
	str += '<input type="tel" maxlength="11" class="form-control" value="'+(item ? item.mobile : '')+'">'
	str += '</div>'
	str += '<div class="input-group">'
	str += '<div class="input-group-text">登录密码</div>'
	str += '<input type="text" maxlength="11" class="form-control" placeholder="'+(item ? '为空不修改密码' :'')+'">'
	str += '</div>'
	$.confirm({
		title:'代理信息',
		content: str,
        buttons:{
        	confirm:{ 
        		text:'确认',
        		btnClass: "btn-primary",
        		action: function(){
            		var input = this.$content.find('input');
            		var data = {name:$(input[0]).val(),mobile:$(input[1]).val(),passwd:$(input[2]).val(),level:$('select').val()};
            		if(data.name == '') return $.notify('名称不能为空');
            		if(data.mobile == '') return $.notify('手机号不能为空');
					if(item) data['aid'] = item.id;
        			showLoading();
	                $.ajax({
	                	url:"/SPAdmin/agent/post",
	                	type:'post',
	                	dataType:'json',
	                	data:data,
	                	success:function(res){
	                		hideLoading();
	                		if(res.code == 1){
	                			$.notify('操作成功');
                                $('#table').bootstrapTable('refresh')
	                		}else $.notify(res.msg);
	                	}
	                });
	            },
        	},
        	cancel:{text:'取消'}
        }
	});
}

</script>
